<template>
  <el-dialog title="项目资料" :visible.sync="dialogFormVisible"
             v-if="dialogFormVisible"
             width="50%"
             :before-close="closeElDialog">
    <el-form ref="fromMessage" :rules="rules" :model="form" label-position="right" label-width="120px" :inline="true">
      <el-form-item label="选择项目" prop="projectId">
        <el-select
          clearable
          v-model="form.projectId"
          filterable
          placeholder="选择项目"
          style="width: 220px;  "
          class="filter-item"
        >
          <el-option
            v-for="item in projects"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资料类型" prop="earlierDataType">
        <el-select
          clearable
          v-model="form.earlierDataType"
          filterable
          placeholder="资料类型"
          style="width: 220px;"
          class="filter-item"
        >
          <el-option
            v-for="item in earlierDataTypes"
            :key="item.id"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件名称" prop="fileName">
        <el-input v-model="form.fileName" style="width: 220px"></el-input>
      </el-form-item>
      <el-form-item label="文件编号" prop="fileSerial">
        <el-input v-model="form.fileSerial" style="width: 220px"></el-input>
      </el-form-item>
      <el-form-item label="申报单位" prop="declareUnitId">
        <el-select
          clearable
          v-model="form.declareUnitId"
          filterable
          placeholder="申报单位"
          style="width: 220px;margin-right: 10px;"
          class="filter-item"
        >
          <el-option
            v-for="item in belongEnterprises"
            :key="item.id"
            :label="item.suoshuqiye"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="批复单位" prop="replyUnitId" label-width="110px">
        <el-select
          clearable
          v-model="form.replyUnitId"
          filterable
          placeholder="项目状态"
          style="width: 220px;margin-right: 10px;"
          class="filter-item"
        >
          <el-option
            v-for="item in belongEnterprises"
            :key="item.id"
            :label="item.suoshuqiye"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="批复日期" prop="replyDate">
        <div class="block">
          <el-date-picker
            v-model="form.replyDate"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="批复金额（万元)">
        <el-input-number style="width: 220px" v-model="form.replyMoney" controls-position="right" @change="handleChange"
                         :min="0"></el-input-number>
      </el-form-item>
      <el-row>
        <el-form-item label="文件上传">
          <el-upload
            multiple
            ref="upload"
            :action="actionUrl"
            :headers="headers"
            :file-list="fileList"
            :auto-upload="false"
            :on-success="handleErrorSuccess"
            :on-error="handleError"
            :data="fileData"
          >
            <!--            :limit="1"-->
            <el-button slot="trigger" size="small" type="primary" >选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload"
                       >上传
            </el-button>
          </el-upload>
        </el-form-item>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          placeholder="请输入内容"
          v-model="form.remark"
          style="width: 573px">
        </el-input>
      </el-form-item>
    </el-form>

    <!--按钮-->
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeElDialog('fromMessage')">取消</el-button>
      <el-button type="primary" @click="confirmSubmit('fromMessage')">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getOptions as  getProject} from "@/api/committee";
import {getDataType  as getEarlierDataTypes  ,getSuoshuqiye as getBelongEnterprises,
} from '@/api/zd'
import {getToken} from "@/utils/auth";
import {putEarlierData} from "@/api/project-data-query";


export default {
  name: "single",
  data() {
    return {
      form: {
        projectId: '',
        earlierDataType:'',
        fileName: '',
        fileSerial: '',
        declareUnitId: '',
        replyUnitId: '',
        replyDate: '',
        replyMoney: '',
        remark: '',
        file_id: '',
      },
      /*文件上传地址*/
      actionUrl: process.env.VUE_APP_BASE_API + "/file/uploadFiles",
      /*文件list*/
      fileList: [],
      headers: {Authorization: 'Bearer ' + getToken()},
      /**/
      dialogFormVisible: false,
      rules: {
        /*sVariety: [
          {required: true, message: '请输入品种', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],*/
      },
      /*字典*/
      /*选择项目*/
      projects: [],
      earlierDataTypes: [],
      belongEnterprises: [],
      fileData: {
        fileType: '',
        relationId: '',
      }

    }


  },
  created() {
    this.getProject();
    this.getEarlierDataTypes();
    this.getBelongEnterprises();

  },
  methods: {
    /*所属企业*/
    getBelongEnterprises() {
      getBelongEnterprises().then(response => {
        this.belongEnterprises = response.data
      })
    },
    /*获取选择的项目*/
    getProject() {
      getProject().then(rep => {
        this.projects = rep.data;
      })
    },
    /*获取资料类型*/
    getEarlierDataTypes() {
      getEarlierDataTypes().then(rep => {
        this.earlierDataTypes = rep.data;
      })
    },

    openElDialog() {
      this.resetForm()
      this.dialogFormVisible = true;
    },
    //确定提交按钮
    confirmSubmit(formName) {
      this.$confirm('确定提交?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitForm(formName);
        //确认
      }).catch(() => {
        //取消
      });
    },
    closeElDialog() {
      this.resetForm()
      this.dialogFormVisible = false;
    },
    submitForm(formName) {
      putEarlierData(this.form).then(rep=>{
        this.form = rep.data;
        this.submitUpload();
      })
    },
    submitUpload() {
      this.fileData.fileType = this.form.id;
      this.fileData.relationId = this.form.id;
      this.$refs.upload.submit()
      this.$refs.fromMessage.resetFields()
      this.dialogFormVisible = false;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleErrorSuccess(response, file, fileList) {
      // 返回值：返回一个新数组，数组元素由回调函数返回的结果组成。
      // let res = list.map(item => item * 2)
      let data = fileList.map(item => {
          if (item.response===undefined){
            return item.id;
          }
        // let parse = JSON.parse(item.response.data);
          // return  [0]
        }
      )
    },
    /**/
    handleChange(value) {
      console.log(value);
    },
    resetForm() {
    },
    handleError(err, file, fileList) {
      this.disabled = false
      console.log(this.disabled, 'disabled')
      this.$message({
        message: err,
        showClose: true,
        type: 'error'
      })
    },
  }


}
</script>

<style scoped>
#formContent {
  margin-top: 15px;
  margin-left: 20px;
}

.formInput {
  width: 350px;
}

.line {
  text-align: center;
}

#goBack {
  margin-bottom: 15px;
}

.el-addForm {
  margin-top: 15px;
}
</style>
